import React, { useState } from 'react';
import { Button, Drawer, message, Tree } from 'antd';
import {updateRoleAuth} from "@/apis/role"
const Auth = ({ closeDrawModel,roleId,roleMenus }) => {
    const [treeData, setTreeData] = useState([
        {
            title: '首页',
            key: '/home/main',
        },
        {
            title: '用户管理',
            key: '/home/user',
        },
        {
            title: '角色管理',
            key: '/home/role',
        },
        {
            title: '店铺管理',
            key: '/home/shop',
        },
        {
            title: '商品管理',
            key: '/home/product',
            children:[
                {
                    title:"商品列表",
                    key:"/home/list"
                },
                {
                    title:"商品分类",
                    key:"/home/category"
                }
            ]
        },
        {
            title: '财务管理',
            key: '/home/charts',
            children:[
                {
                    title:"销售数据",
                    key:"/home/sale"
                },
                {
                    title:"工资流水",
                    key:"/home/salary"
                }
            ]
        }
    ])
    const [menus,setMenus] = useState([])
    const onClose = () => {
        closeDrawModel()
    };
    const checkedEvent = (values)=>{
        console.log(values);
        setMenus(values)
    }
    const addAuth =async ()=>{
        //发送异步请求后端
        console.log(roleId,roleMenus);
        const myRole = {
            id:roleId,
            authTime:new Date(),
            authUser:"xiaowang",
            menus:menus
        }

        const res = await updateRoleAuth(myRole)
        if(res.data.code){
            message.success("授权成功")
        }else{
            message.error("授权失败")
        }


    }
    return (
        <>
            <Drawer title="Basic Drawer" placement="right" onClose={onClose} open={true}>
                <Tree
                    checkable
                    defaultExpandedKeys={['/home/product', '/home/charts']}
                    defaultCheckedKeys={roleMenus}
                    treeData={treeData}
                    onCheck = {checkedEvent}
                />
                <Button onClick={addAuth}>授权</Button>
            </Drawer>
        </>
    );
};
export default Auth;